﻿using System;
using System.Collections.Generic;
using Resources;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.UserControls
{
    public partial class TestRequestControl : System.Web.UI.UserControl
    {
        public string Language
        {
            get { return BasePage.Language.ToString(); }
        }
        protected BasePage BasePage
        {
            get
            {
                return this.Page as BasePage;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (BasePage.PageStatus == PageStatus.Readonly)
                {
                    Wicresoft.RDW.Utility.Helper.ControlEditable(this, false);
                }
                else
                {
                    Wicresoft.RDW.Utility.Helper.ControlEditable(this, true);
                }
                InitResource();
                InitControl();
                LoadData();
            }

        }
        protected void InitControl()
        {
            //Load Current User
            Model.User user = new Model.User();
            if (BasePage.Id != 0)
            {
                var request = RequestPresent.GetById(BasePage.Id);
                user = UserPresent.GetById(request.RequestUserId);
            }
            else
            {
                user = UserPresent.GetById(BasePage.CurrentUserId);
            }
            if (user != null)
            {
                txtUserName.Text = BasePage.Language.ToString() == "CN" ? user.NameCN : user.NameEN;
                txtDep.Text = user.BD;
                txtCellphone.Text = user.Mobile;
                txtTelephone.Text = user.OfficePhone;
                txtEmail.Text = user.Email;
                txtUserName.ReadOnly = true;
                txtDep.ReadOnly = true;
                txtCellphone.ReadOnly = true;
                txtTelephone.ReadOnly = true;
                txtEmail.ReadOnly = true;
            }
        }
        protected void InitResource()
        {
            rfvTestContent.ErrorMessage = NameResource.Test_Content + CommonResource.NotNull;
            rfvTesterName.ErrorMessage = NameResource.User_Name + CommonResource.NotNull;
            rfvPersonNumber.ErrorMessage = NameResource.Test_Number + CommonResource.NotNull;
            revTesterEmail.ErrorMessage = NameResource.User_Email + CommonResource.ErrorFormat;
            revTester2Email.ErrorMessage = NameResource.User_Email + CommonResource.ErrorFormat;
            revTester3Email.ErrorMessage = NameResource.User_Email + CommonResource.ErrorFormat;
            revTesterCellphone.ErrorMessage = NameResource.User_Cellphone + CommonResource.ErrorFormat;
            revTester2Cellphone.ErrorMessage = NameResource.User_Cellphone + CommonResource.ErrorFormat;
            revTester3Cellphone.ErrorMessage = NameResource.User_Cellphone + CommonResource.ErrorFormat;
            rvPersonNumber.ErrorMessage = NameResource.Test_Number + CommonResource.ErrorFormat;
        }
        protected void LoadData()
        {
            if (BasePage.PageStatus == PageStatus.New)
                return;
            Model.TestRequest testRequest = TestRequestPresent.GetByCondition(p => p.RequestId == BasePage.Id);
            if (testRequest != null)
            {
                hfTestRequestId.Value = testRequest.TestRequestId.ToString();

                List<Model.TestPerson> testPerson = TestPersonPresent.GetList(p => p.TestRequestId == testRequest.TestRequestId);

                hfTesterId.Value = testPerson[0].Id.ToString();
                txtTesterName.Text = testPerson[0].Name;
                txtTesterDep.Text = testPerson[0].Dep;
                txtTesterCellphone.Text = testPerson[0].Mobile;
                txtTesterTelephone.Text = testPerson[0].Telephone;
                txtTesterEmail.Text = testPerson[0].Email;
                if (testPerson.Count > 1)
                {
                    hfTester2Id.Value = testPerson[1].Id.ToString();
                    txtTester2Name.Text = testPerson[1].Name;
                    txtTester2Dep.Text = testPerson[1].Dep;
                    txtTester2Cellphone.Text = testPerson[1].Mobile;
                    txtTester2Telephone.Text = testPerson[1].Telephone;
                    txtTester2Email.Text = testPerson[1].Email;
                    if (testPerson.Count > 2)
                    {
                        hfTester3Id.Value = testPerson[2].Id.ToString();
                        txtTester3Name.Text = testPerson[2].Name;
                        txtTester3Dep.Text = testPerson[2].Dep;
                        txtTester3Cellphone.Text = testPerson[2].Mobile;
                        txtTester3Telephone.Text = testPerson[2].Telephone;
                        txtTester3Email.Text = testPerson[2].Email;
                    }
                    else
                    {
                        txtTester3Name.Text = "";
                        txtTester3Dep.Text = "";
                        txtTester3Cellphone.Text = "";
                        txtTester3Telephone.Text = "";
                        txtTester3Email.Text = "";
                    }
                    phScript.Visible = true;
                }

                txtOtherRequirments.Text = Server.HtmlDecode(testRequest.OtherRequirements);
                txtTestContent.Text = Server.HtmlDecode(testRequest.Content);
                txtPersonNumber.Text = testRequest.PersonNum == 0 ? "" : testRequest.PersonNum.ToString();
            }

        }

        public Model.TestRequest TestRequestData
        {
            get
            {
                int personNumber;
                bool result = int.TryParse(txtPersonNumber.Text, out personNumber);
                Model.TestRequest data = new Model.TestRequest()
                {
                    RequestId = BasePage.Id,
                    Content = Server.HtmlEncode(txtTestContent.Text),
                    PersonNum = personNumber,
                    OtherRequirements = Server.HtmlEncode(txtOtherRequirments.Text)
                };
                if (!string.IsNullOrEmpty(hfTestRequestId.Value))
                {
                    data.TestRequestId = Convert.ToInt32(hfTestRequestId.Value);
                }
                else
                {
                    data.TestRequestId = 0;
                }
                if (BasePage.Id == 0)
                {
                    data.TestPerson.Add(TestPersonData);
                    if (!string.IsNullOrEmpty(TestPerson2Data.Name))
                    {
                        data.TestPerson.Add(TestPerson2Data);
                    }
                    if (!string.IsNullOrEmpty(TestPerson3Data.Name))
                    {
                        data.TestPerson.Add(TestPerson3Data);
                    }
                }
                data.RequestId = BasePage.Id;
                return data;
            }
        }
        public void TestPersonUpdate()
        {
            var testPersonList = TestPersonPresent.GetList(p => p.TestRequestId == TestRequestData.TestRequestId);
            var testPerson1 = TestPersonPresent.GetById(TestPersonData.Id);
            var testPerson2 = TestPersonPresent.GetById(TestPerson2Data.Id);
            var testPerson3 = TestPersonPresent.GetById(TestPerson3Data.Id);

            TestPersonData.TestRequestId = TestRequestData.TestRequestId;
            TestPersonData.CopeProperties(testPerson1);

            if (testPerson2 != null)
            {
                TestPerson2Data.TestRequestId = TestRequestData.TestRequestId;
                TestPerson2Data.CopeProperties(testPerson2);
            }

            if (testPerson3 != null)
            {
                TestPerson3Data.TestRequestId = TestRequestData.TestRequestId;
                TestPerson3Data.CopeProperties(testPerson3);
            }

            TestPersonPresent.Update(testPerson1);
            if (testPersonList.Count > 1)
            {
                if (!string.IsNullOrEmpty(TestPerson2Data.Name))
                {
                    TestPersonPresent.Update(testPerson2);
                }
                else
                {
                    TestPersonPresent.Delete(testPersonList[1].Id);
                }
                if (testPersonList.Count > 2)
                {
                    if (!string.IsNullOrEmpty(TestPerson3Data.Name))
                    {
                        TestPersonPresent.Update(testPerson3);
                    }
                    else
                    {
                        TestPersonPresent.Delete(testPersonList[2].Id);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(TestPerson3Data.Name))
                    {
                        TestPerson3Data.TestRequestId = TestRequestData.TestRequestId;
                        TestPersonPresent.Create(TestPerson3Data);
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(TestPerson2Data.Name))
                {
                    TestPerson2Data.TestRequestId = TestRequestData.TestRequestId;
                    TestPersonPresent.Create(TestPerson2Data);
                }
                if (!string.IsNullOrEmpty(TestPerson3Data.Name))
                {
                    TestPerson3Data.TestRequestId = TestRequestData.TestRequestId;
                    TestPersonPresent.Create(TestPerson3Data);
                }
            }
        }
        private Model.TestPerson testPerson = new Model.TestPerson();
        private Model.TestPerson TestPersonData
        {
            get
            {
                testPerson.Name = txtTesterName.Text;
                testPerson.Dep = txtTesterDep.Text;
                testPerson.Mobile = txtTesterCellphone.Text;
                testPerson.Telephone = txtTesterTelephone.Text;
                testPerson.Email = txtTesterEmail.Text;

                if (!string.IsNullOrEmpty(hfTesterId.Value))
                {
                    testPerson.Id = Convert.ToInt32(hfTesterId.Value);
                }
                else
                {
                    testPerson.Id = 0;
                }
                return testPerson;
            }
            set
            {
                testPerson = value;
            }
        }
        private Model.TestPerson testPerson2 = new Model.TestPerson();
        private Model.TestPerson TestPerson2Data
        {
            get
            {
                testPerson2.Name = txtTester2Name.Text;
                testPerson2.Dep = txtTester2Dep.Text;
                testPerson2.Mobile = txtTester2Cellphone.Text;
                testPerson2.Telephone = txtTester2Telephone.Text;
                testPerson2.Email = txtTester2Email.Text;

                if (!string.IsNullOrEmpty(hfTester2Id.Value))
                {
                    testPerson2.Id = Convert.ToInt32(hfTester2Id.Value);
                }
                else
                {
                    testPerson2.Id = 0;
                }
                return testPerson2;
            }
            set
            {
                testPerson2 = value;
            }
        }

        private Model.TestPerson testPerson3 = new Model.TestPerson();
        private Model.TestPerson TestPerson3Data
        {
            get
            {
                testPerson3.Name = txtTester3Name.Text;
                testPerson3.Dep = txtTester3Dep.Text;
                testPerson3.Mobile = txtTester3Cellphone.Text;
                testPerson3.Telephone = txtTester3Telephone.Text;
                testPerson3.Email = txtTester3Email.Text;

                if (!string.IsNullOrEmpty(hfTester3Id.Value))
                {
                    testPerson3.Id = Convert.ToInt32(hfTester3Id.Value);
                }
                else
                {
                    testPerson3.Id = 0;
                }
                return testPerson3;
            }
            set
            {
                testPerson3 = value;
            }
        }
    }
}